export default {
	data() {
		return {
			curData: {},
			modalTitle: "",
			modalText: "",
			modalTips: "",
			modalPrice: {}
		}
	},
	methods: {
		//确认对话框点击确定触发
		onModalConfirm() {
			//根据弹出对话框时定义的modalConfirmFn决定执行事件
			this.$util.throttle(async () => {
				this[this.modalConfirmFn]();
			})
		},
		//支付订单
		async pay(data, type) {
			data.money = data.price;
			data.uid = uni.getStorageSync('uid');
			const res = await this.$request("goods/pay", data, {
				showLoading: true
			});
			if (res.result == 0) {
				this.PayBywx(res.data.body, type,data.index);
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		PayBywx(data,type,index) {
			console.log(data)
			let _this = this;
			if (data) {
				uni.requestPayment({
					timeStamp: data.timestamp,
					nonceStr: data.noncestr,
					package: `prepay_id=${data.prepayid}`,
					signType: data.signType,
					paySign: data.sign,
					success(res) {
						if (type == 'creat') {
							uni.redirectTo({
								url: `/pages/order/paySuccess`
							})
						} else if (type == "list") {
							_this.dataList.splice(index, 1);
							if (_this.dataList.length == 0) {
								_this.$refs.pullScroll && _this.$refs.pullScroll.empty();
							}
							uni.navigateTo({
								url: `/pages/order/paySuccess`
							})
						} else {
							uni.redirectTo({
								url: `/pages/order/paySuccess`
							})
						}
					},
					fail(res) {
						_this.$util.msg('订单支付取消');
						if (type == 'creat') {
							setTimeout(() => {
								uni.redirectTo({
									url: "/pages/order/list?type=0"
								})
							}, 500)
						}
					}
				});
			}
		},
		//取消订单弹窗确认
		cancelOrder(data) {
			console.log(data)
			this.modalTitle = "取消订单";
			this.modalText = '您是否确定要取消订单';
			this.modalTips = "";
			this.modalPrice = {};
			this.modalConfirmFn = 'cancelOrderConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//取消订单提交
		async cancelOrderConfirm() {
			const res = await this.$request('goods/cancel', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//删除订单弹窗确认
		deleteOrder(data) {
			console.log(data)
			this.modalTitle = "删除订单";
			this.modalText = '您是否确定要删除订单';
			this.modalTips = "";
			this.modalPrice = {};
			this.modalConfirmFn = 'deleteOrderConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//删除订单提交
		async deleteOrderConfirm() {
			const res = await this.$request('goods/del', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//取消申请退款弹窗确认
		cancelRequestOrder(data) {
			console.log(data)
			this.modalTitle = "取消申请";
			this.modalText = '您确认要取消申请吗?';
			this.modalTips = "";
			this.modalPrice = {};
			this.modalConfirmFn = 'cancelRequestConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//取消申请退款提交
		async cancelRequestConfirm() {
			const res = await this.$request('goods/cancel_refund', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//取消发货弹窗确认
		cancelDeliveryOrder(data) {
			console.log(data)
			this.modalTitle = "取消申请";
			this.modalText = '您确认要取消申请发货吗?';
			this.modalTips = "";
			this.modalPrice = {};
			this.modalConfirmFn = 'cancelDeliveryConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//取消发货提交
		async cancelDeliveryConfirm() {
			const res = await this.$request('goods/deliver-cancel', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//委托代卖弹窗确认
		async entrustSellOrder(data) {
			console.log(data)
			const res = await this.$request('goods/entrust-before', {
				id: data.id
			});
			if (res.result == 0) {
				this.modalText = "";
				this.modalTitle = "委托代售";
				this.modalTips = `委托售卖平台加价${res.data.proportion}%，当用户购买并验收通过之后，则平台抽取加价费用的${res.data.extractProportion}%`;
				this.modalPrice = res.data;
				console.log(this.modalPrice)
				this.modalConfirmFn = 'entrustSellConfirm';
				this.curData = data;
				this.$refs.mixModal.open();
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//委托代卖提交
		async entrustSellConfirm() {
			const res = await this.$request('goods/entrust', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage();
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//申请发货
		requestDeliveryOrder(data) {
			console.log(data)
			this.modalTitle = "申请发货";
			this.modalText = '您确认要申请发货吗?';
			this.modalTips = "温馨提示：申请发货之后，商品将通过快递邮寄给您商品发货之后，将不可以在委托平台进行售卖，请谨慎处理";
			this.modalPrice = {};
			this.modalConfirmFn = 'requestDeliveryConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//申请发货提交
		async requestDeliveryConfirm() {
			const res = await this.$request('goods/deliver', {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}, {
				showLoading: true
			})
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote);
			}
		},
		//申请退款提交
		refund(data) {
			this.$util.throttle(async () => {
				const res = await this.$request('goods/refund', {
					reason: data.reason,
					reasonId: data.reasonId,
					uid: uni.getStorageSync('uid'),
					id: data.id
				}, {
					showLoading: true
				})
				if (res.result == 0) {
					this.$util.msg('提交成功!');
					uni.navigateBack()
				} else {
					this.$util.msg(res.resultNote)
				}
			})
		},
		//确认收货 弹窗确认
		confirmReceipt(data) {
			this.modalTitle = "确认收货";
			this.modalPrice = {};
			this.modalTips = "";
			this.modalText = '请确保您已收到商品，并检查无损后再确认收货';
			this.modalConfirmFn = 'confirmReceiptConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		//  确认收货
		async confirmReceiptConfirm() {
			const data = {
				uid: uni.getStorageSync('uid'),
				id: this.curData.id
			}
			const res = await this.$request('goods/receive', data);
			this.$util.msg(res.resultNote);
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote)
			}
		},
		//取消退款 弹窗确认
		confirmReplay(data) {
			this.modalTitle = "取消退款";
			this.modalPrice = {};
			this.modalTips = "";
			this.modalText = '确认取消吗?';
			this.modalConfirmFn = 'confirmReplayConfirm';
			this.curData = data;
			this.$refs.mixModal.open();
		},
		async confirmReplayConfirm() {
			const data = {
				uid: uni.getStorageSync('uid'),
				orderNumber: this.curData.id
			}
			const res = await this.$request('order/cancelOrderRefund', data);
			if (res.result == 0) {
				this.userComPage()
			} else {
				this.$util.msg(res.resultNote);
			}
		},
		userComPage() {
			if (this.curData.type == 'list') {
				console.log(this.current)
				if(this.current!=0){
					this.dataList.splice(this.curData.index, 1);
					if (this.dataList.length == 0) {
						this.$refs.pullScroll.empty();
					}
				}else{
					this.refresh()
				}
			} else {
				uni.navigateBack();
			}
		}

	}
}
